/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 16:44:42
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.util.XyUtil;

public class AirManager {
    public final static String ID = "id";

    public final static String AIR_NUM = "air_num";

    public final static String START_CITY = "start_city";

    public final static String END_CITY = "end_city";

    public final static String START_PLACE = "start_place";

    public final static String END_PALCE = "end_place";

    public final static String COMPANY = "company";

    public final static String START_TIME = "start_time";

    public final static String END_TIME = "end_time";

    public static final String TABLE_NAME = "tb_air";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS "
            + TABLE_NAME;

    public static final String CREATE_TABLE = "create table  if not exists "
            + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY," + AIR_NUM
            + " TEXT not null unique," + START_CITY + " TEXT," + END_CITY
            + " TEXT," + START_PLACE + " TEXT," + END_PALCE + " TEXT,"
            + START_TIME + " TEXT," + END_TIME + " TEXT," + COMPANY + " TEXT"
            + ")";

    /***
     * 导入航班数据
     * 
     * @param ctx
     */
    public static void importAirData(Context ctx) {
        SQLiteDatabase db = null;
        String path = Constant.getDRAWBLE_PATH() + "air_data.txt";
        java.io.LineNumberReader r = null;
        try {
            r = XyUtil.getLineByCompressFile(path);
            String line = null;
            db = DBManager.getSQLiteDatabase();
            db.beginTransaction();
            line = r.readLine();
            if (line == null) {
                return;
            }
            String[] airArr = line.split("=");
            long rr = 0;
            while ((line = r.readLine()) != null) {
                String[] arr = line.split(" ");
                if (arr.length < 3) {
                    continue;
                }
                ContentValues newValues = new ContentValues();
                newValues.put(AIR_NUM, arr[0]);
                newValues.put(START_PLACE, airArr[Integer.valueOf(arr[1])]);
                newValues.put(END_PALCE, airArr[Integer.valueOf(arr[2])]);

                rr = db.update(TABLE_NAME, newValues, AIR_NUM + "=?",
                        new String[] { arr[0] });
                if (rr < 1) {
                    rr = db.insert(TABLE_NAME, null, newValues);
                }
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "importAirData error:", e);
            /*SDK-550 hewengao 20160719 start*/
        } finally {
            DBManager.closeDB(path, true, r, null, db);
        }
    }

    public static boolean checkUpdateData() {

        new Thread() {
            @Override
            public void run() {
            	try {
            		importAirData(Constant.getContext());
				} catch (Throwable e) {
					LogManager.w(Constant.TAG, e.getMessage(), e);
				}

            }
        }.start();
        return true;
    }

    /**
     * �>ݺ���Ż�ȡ���
     * 
     * @param trainNum
     * @return
     */
    public static String[] queryStartEndPlace(String airNum) {

        XyCursor cursor = null;
        try {
            cursor = DBManager
                    .query(TABLE_NAME, new String[] { START_PLACE, END_PALCE },
                            AIR_NUM + " = ?", new String[] { airNum + "" });
            if (cursor != null && cursor.moveToNext()) {
                int sindex = cursor.getColumnIndex(START_PLACE);
                int eindex = cursor.getColumnIndex(END_PALCE);
                String s = cursor.getString(sindex);
                String e = cursor.getString(eindex);
                return new String[] { s, e };
            }
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.w(Constant.TAG,"AirManager queryStartEndPlace error:", e);
        } finally {
            XyCursor.closeCursor(cursor, true);
        }
        return null;
    }
}
